﻿    /// <reference path="../vsdoc/jquery-1.8.3-vsdoc.js" />

/*
** settings for TAB browsing
*/

/* Menu */
var MenuCookieSelectedAside = "menu_aside_selected";
asyncHandlersForEach.push(AppendMenuSelectors);
function AppendMenuSelectors() 
{
    $(".masterRibb .menu A").click(function () 
    {
        // read target id from data attribute
        var id = $(this).data("target");
        var target = $("#" + id);
        if (target.length != 1) 
        {
            return;
        }
        // hide others && show the target
        $(".masterMenu ASIDE, .masterMenu DIV.aside").hide();
        target.show();

        // store selected index in the cookie
        var index = $(target).prevAll(target.tagName).size();
        $.cookie(MenuCookieSelectedAside, index, { expires: 7, path: '/' });

        TabHandler(target, $);
    });
}

/*** TAB browsing BEGIN ***/
var notSelectedTabCssClassName = "notSelectedTab";

function FindSelectedTabCookieName(item) { return "tab" + item.siblings("INPUT[name=tabs]").attr("value") }
function SetSelectedTab(item, index)
{
    try
    {
        if (index == null) { index = 0; }
        item.siblings(item.get(0).tagName).each(function () { $(this).addClass(notSelectedTabCssClassName); });     // decorate all tabs
        var tag = item.parent().next()[0].tagName;
        item.parent().nextUntil(":not("+tag+")").each(function () { $(this).addClass(notSelectedTabCssClassName); }); // decorate all contents
        item.removeClass(notSelectedTabCssClassName)                                            // remove NOT selected from current tab
        var current = item.parent().nextAll().eq(index);
        current.removeClass(notSelectedTabCssClassName);      // and remove it also from the content
        return current;
    } catch (e) { }
}

asyncHandlersForEach.push(AppendTabBrowsingListneres);
function AppendTabBrowsingListneres()
{
    $("INPUT[name=tabs]").each(function (e)
    {         // page load
        var index = $.cookie("tab" + $(this).attr("value"));                // index stored in the cookie
        SetSelectedTab($(this).nextAll().eq(index), index);   // set CSS for NOT selected
    });
}

//$(document).ready(function ()
asyncHandlersForEach.push(AppendTabBrowsingSwitch);
function AppendTabBrowsingSwitch()
{
    $("INPUT[name=tabs]").siblings("SPAN").click(function (e)
    {                   // tab clicked
        var index = $(this).prevAll(this.tagName).size();                           // index based on clicked element
        $.cookie(FindSelectedTabCookieName($(this)), index, { expires: 7, path: '/' }); // store selected index in the cookie
        var current = SetSelectedTab($(this), index);                               // set CSS for NOT selected
        TabHandler(current, $); // could be used for some other handling (async loads)
    });
}
//});

/*** TAB browsing END ***/